package cn.lingyangwl.agile.pay.client.config;

import cn.lingyangwl.agile.pay.client.constants.GoodsMqCons;
import cn.lingyangwl.agile.pay.client.model.mq.GoodsOrderCloseMQ;
import cn.lingyangwl.agile.pay.client.model.mq.GoodsOrderRefundMQ;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 延时消息队列
 *
 * @author shenguangyang
 */
@Slf4j
@Lazy(value = false)
@Configuration
public class GoodsOrderMqDelayedConfig {
    @Resource
    private RabbitAdmin rabbitAdmin;

//    @PostConstruct
//    public void init() {
//        log.info("init {}", this.getClass().getName());
//        rabbitAdmin.declareExchange(delayedExchange());
//        rabbitAdmin.declareQueue(goodsOrderCloseDelayedQueue());
//        rabbitAdmin.declareQueue(goodsRefundDelayQueue());
//    }

    /**
     * 自定义交换机 我们在这里定义的是一个延迟交换机
     * 需要在rabbitmq上安装延时插件
     * 在官网上下载 <a href="https://www.rabbitmq.com/community-plugins.html">官网</a>，下载
     * rabbitmq_delayed_message_exchange 插件
     */
    @Bean
    public CustomExchange goodsOrderDelayedExchange() {
        Map<String, Object> args = new HashMap<>();
        // 自定义交换机的类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(GoodsMqCons.ORDER_EVENT_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue goodsOrderCloseDelayedQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(GoodsOrderCloseMQ.DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding orderDelayedBinding(@Qualifier("goodsOrderCloseDelayedQueue") Queue queue,
                                       @Qualifier("goodsOrderDelayedExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(GoodsOrderCloseMQ.DELAY_ROUTING_KEY).noargs();
    }


    /**
     * 支付退款-延时队列
     */
    @Bean
    public Queue goodsRefundDelayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        return new Queue(GoodsOrderRefundMQ.QUEUE, true, false, false, arguments);
    }

    @Bean
    public Binding payOrderMchNotifyDelayBinding(@Qualifier("goodsRefundDelayQueue") Queue queue,
                                       @Qualifier("goodsOrderDelayedExchange") CustomExchange exchange) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with(GoodsOrderRefundMQ.ROUTING_KEY).noargs();
    }
}
